package main

import (
	"encoding/hex"
	"fmt"
	"time"
)

// parseHexTime 解析16进制时间字符串
func parseHexTime(hexStr string) (time.Time, error) {
	// 移除16进制字符串中的分隔符
	cleanHexStr := hexStr[0:2] + hexStr[3:5] + hexStr[6:8] + hexStr[9:11] + hexStr[12:14] + hexStr[15:17] + hexStr[18:20] + hexStr[21:23] + hexStr[24:26] + hexStr[27:29]

	// 将清理后的字符串转换为字节
	data, err := hex.DecodeString(cleanHexStr)
	if err != nil {
		return time.Time{}, err
	}

	// 解析字节为时间组件
	year := int(data[0])<<8 + int(data[1])
	month := time.Month(data[2])
	day := int(data[3])
	hour := int(data[4])
	minute := int(data[5])
	second := int(data[6])
	microsecond := int(data[7])<<16 + int(data[8])<<8 + int(data[9])
	direction := data[10]
	hoursFromUTC := int(data[11])
	minutesFromUTC := int(data[12])

	// 创建时间对象
	loc := time.FixedZone(fmt.Sprintf("UTC%+d:%02d", hoursFromUTC, minutesFromUTC), hoursFromUTC*3600+minutesFromUTC*60)
	if direction == '-' {
		loc = time.FixedZone(fmt.Sprintf("UTC-%02d:%02d", hoursFromUTC, minutesFromUTC), -hoursFromUTC*3600-minutesFromUTC*60)
	}

	t := time.Date(year, month, day, hour, minute, second, microsecond*1000, loc)
	return t, nil
}

func main() {
	hexTime := "07-e7-03-11-10-16-0e-00-00-00-2d-00-00"
	t, err := parseHexTime(hexTime)
	if err != nil {
		fmt.Println("Error parsing hex time:", err)
		return
	}
	fmt.Println("Parsed time:", t)
}
